package com.neudu.glsb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.glsb.mapper.AbnormalOrderMapper;
import com.neudu.glsb.pojo.AbnormalOrder;
import com.neudu.glsb.service.AbnormalOrderService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Map;

/**
 * 异常订单Service实现类
 */
@Service
public class AbnormalOrderServiceImpl extends ServiceImpl<AbnormalOrderMapper, AbnormalOrder> implements AbnormalOrderService {

    @Autowired
    private AbnormalOrderMapper abnormalOrderMapper;

    /**
     * 上传异常订单
     */
    @Override
    public boolean uploadAbnormalOrder(AbnormalOrder abnormalOrder) {
        // 自动设置提交时间
        abnormalOrder.setCreateTime(new Date());
        // 默认为"待处理"状态
        if (ObjectUtils.isEmpty(abnormalOrder.getHandleStatus())) {
            abnormalOrder.setHandleStatus("待处理");
        }
        return save(abnormalOrder);
    }

    /**
     * 员工查询自己的异常订单
     */
    @Override
    public PageInfo<AbnormalOrder> getAbnormalOrderListByCreateBy(Map<String, Object> map) {
        // 分页参数
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);

        // 调用Mapper查询
        Page<AbnormalOrder> page = abnormalOrderMapper.selectByCreateBy(map);
        return new PageInfo<>(page);
    }

    /**
     * 管理员查询所有异常订单
     */
    @Override
    public PageInfo<AbnormalOrder> getAllAbnormalOrderList(Map<String, Object> map) {
        // 分页参数
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);

        // 调用Mapper查询
        Page<AbnormalOrder> page = abnormalOrderMapper.selectAllByParams(map);
        return new PageInfo<>(page);
    }

    /**
     * 处理异常订单
     */
    @Override
    public boolean handleAbnormalOrder(Integer id, String handleStatus, String handleBy, String handleRemark) {
        // 参数校验
        if (ObjectUtils.isEmpty(id) || ObjectUtils.isEmpty(handleStatus)
                || ObjectUtils.isEmpty(handleBy) || ObjectUtils.isEmpty(handleRemark)) {
            return false;
        }

        // 调用Mapper更新
        int rows = abnormalOrderMapper.updateHandleStatus(
                id, handleStatus, handleBy, handleRemark, new Date()
        );
        return rows > 0;
    }
}